package com.cws.project.cws.score.controller;

import java.util.List;

import com.cws.common.utils.StringUtils;
import com.cws.common.utils.file.FileUtils;
import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.config.CwsConfig;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.common.CommonController;
import com.cws.project.system.user.domain.User;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cws.project.cws.score.domain.HomeworkScore;
import com.cws.project.cws.score.service.IHomeworkScoreService;

import javax.servlet.http.HttpServletResponse;

/**
 * 作业评分Controller
 *
 * @author xyl
 * @date 2022-01-12
 */
@Controller
@RequestMapping("/cws/score")
public class HomeworkScoreController extends BaseController {
    private String prefix = "cws/score";

    private static final Logger log = LoggerFactory.getLogger(HomeworkScoreController.class);

    @Autowired
    private IHomeworkScoreService homeworkScoreService;

    @RequiresPermissions("cws:score:view")
    @GetMapping()
    public String score() {
        return prefix + "/score";
    }

    @RequiresPermissions("cws:score:view")
    @GetMapping("/list/{homeworkId}")
    public String listByHomeworkId(@PathVariable("homeworkId") Long homeworkId, ModelMap mmap) {
        mmap.put("homeworkId",homeworkId);
        return prefix + "/score";
    }

    /**
     * 查询作业评分列表
     */
    @RequiresPermissions("cws:score:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HomeworkScore homeworkScore) {
        startPage();
        User sysUser = getSysUser();
        //教师
        if(sysUser.isTeacher() ){
            homeworkScore.setTeacherId(sysUser.getUserId());
        }
        List<HomeworkScore> list = homeworkScoreService.selectHomeworkScoreList(homeworkScore);
        return getDataTable(list);
    }


    /**
     * 导出作业评分列表
     */
    @RequiresPermissions("cws:score:export")
    @Log(title = "作业评分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HomeworkScore homeworkScore) {
        List<HomeworkScore> list = homeworkScoreService.selectHomeworkScoreList(homeworkScore);
        ExcelUtil<HomeworkScore> util = new ExcelUtil<HomeworkScore>(HomeworkScore.class);
        return util.exportExcel(list, "score");
    }

    //    @RequiresPermissions("cws:score:download")
//    @Log(title = "作业下载", businessType = BusinessType.DOWNLOAD)
    @GetMapping("/download/{scoreId}")
    public void download(@PathVariable("scoreId") Long scoreId, HttpServletResponse response) throws Exception {
        HomeworkScore homeworkScore = homeworkScoreService.selectHomeworkScoreById(scoreId);
        if(ObjectUtils.isEmpty(homeworkScore)){
            throw new Exception("非法请求！");
        }
        try {
            String filePath = homeworkScore.getHomeworkUrl().replace("http://localhost:8080/profile/", "D:/cws/uploadPath/");
            String fix = filePath.substring(filePath.lastIndexOf("."));
            String realFileName = homeworkScore.getHomeworkTopic() + "-" + homeworkScore.getLoginName() + "-" + homeworkScore.getUserName() + fix;
            FileUtils.setAttachmentResponseHeader(response, realFileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 新增作业评分
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存作业评分
     */
    @RequiresPermissions("cws:score:add")
    @Log(title = "作业评分", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HomeworkScore homeworkScore) {
        return toAjax(homeworkScoreService.insertHomeworkScore(homeworkScore));
    }


    /**
     * 学生保存作业
     */
    @RequiresPermissions("cws:homework:student:submit")
    @Log(title = "学生提交作业", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(HomeworkScore homeworkScore) {
        homeworkScore.setUserId(getUserId());
        return toAjax(homeworkScoreService.submitHomework(homeworkScore));
    }

    /**
     * 修改作业评分
     */
    @GetMapping("/edit/{scoreId}")
    public String edit(@PathVariable("scoreId") Long scoreId, ModelMap mmap) {
        HomeworkScore homeworkScore = homeworkScoreService.selectHomeworkScoreById(scoreId);
        mmap.put("homeworkScore", homeworkScore);
        return prefix + "/edit";
    }

    /**
     * 修改保存作业评分
     */
    @RequiresPermissions("cws:score:edit")
    @Log(title = "作业评分", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HomeworkScore homeworkScore) {
        return toAjax(homeworkScoreService.updateHomeworkScore(homeworkScore));
    }

    /**
     * 删除作业评分
     */
    @RequiresPermissions("cws:score:remove")
    @Log(title = "作业评分", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(homeworkScoreService.deleteHomeworkScoreByIds(ids));
    }
}
